<template>
  <el-card>
    <div class="header">
      <el-button type="primary" @click="openDialog()">新增公告</el-button>
    </div>

    <el-table :data="noticeList" border style="width: 100%" v-loading="loading">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="title" label="标题" />
      <el-table-column prop="detaile" label="内容" />
      <el-table-column prop="status" label="状态">
        <template #default="{ row }">
          <el-tag :type="row.status === 'active' ? 'success' : 'info'">
            {{ row.status === 'active' ? '启用' : '停用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="220">
        <template #default="{ row }">
          <el-button size="small" @click="openDialog(row)">编辑</el-button>
          <el-button size="small" type="danger" @click="deleteNotice(row.id)">删除</el-button>
          <el-button size="small" type="warning" @click="toggleStatus(row)">
            {{ row.status === 'active' ? '停用' : '启用' }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="form.id ? '编辑公告' : '新增公告'" v-model="dialogVisible" width="600px">
      <el-form :model="form" label-width="80px">
        <el-form-item label="标题">
          <el-input v-model="form.title" />
        </el-form-item>
        <el-form-item label="内容">
          <el-input type="textarea" v-model="form.detaile" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveNotice">保存</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>


<script setup>
import { useAnnouncement } from '@/hooks/institute/useAnnouncement';
import { onMounted } from 'vue';

const {
  loading,
  noticeList,
  dialogVisible,
  form,
  openDialog,
  saveNotice,
  deleteNotice,
  toggleStatus,
} = useAnnouncement();


</script>


<style scoped>
.header {
  margin-bottom: 16px;
  display: flex;
  justify-content: space-between;
}
</style>